package com.androidhive.androidlistview;

import java.util.ArrayList;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;

import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
 
public class ListViewCheckboxesActivity extends Activity {
	 public final static String fields = "com.example.sample.MESSAGE";
	 public final static String crops = null;
	 public String crp=null;
	
 
 MyCustomAdapter dataAdapter = null;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_list_view_checkboxes);
 
  //Generate list View from ArrayList
  //Intent intent = getIntent();
 
  displayListView();
  checkButtonClick();
 
  
 
 }
 
 private void displayListView() {
 
  //Array list of countries
  ArrayList<Country> countryList = new ArrayList<Country>();
  Country country = new Country("Crop Name",false);
  countryList.add(country);
  country = new Country("Crop Quality",true);
  countryList.add(country);
  country = new Country("Crop Quantity",true);
  countryList.add(country);
  country = new Country("BaseCost/Ton(Rs.)",true);
  countryList.add(country);
  country = new Country("Current Bid Price(Rs.)",true);
  countryList.add(country);
  country = new Country("Farmer",true);
  countryList.add(country);
  country = new Country("Location(State)",true);
  countryList.add(country);
  country = new Country("Package Cost/Ton(Rs.)",true);
  countryList.add(country);
 
  //create an ArrayAdaptar from the String Array
  dataAdapter = new MyCustomAdapter(this,
    R.layout.country_info, countryList);
  ListView listView = (ListView) findViewById(R.id.listView1);
  // Assign adapter to ListView
  listView.setAdapter(dataAdapter);
 
 
  listView.setOnItemClickListener(new OnItemClickListener() {
   public void onItemClick(AdapterView<?> parent, View view,
     int position, long id) {
    // When clicked, show a toast with the TextView text
    Country country = (Country) parent.getItemAtPosition(position);
    Toast.makeText(getApplicationContext(),
      "Clicked on Row: " + country.getName(),
      Toast.LENGTH_LONG).show();
   }
  });
 Log.d("ListViewCheckboxesActivity","1");
 }
 
 private class MyCustomAdapter extends ArrayAdapter<Country> {
 
  private ArrayList<Country> countryList;
 
  public MyCustomAdapter(Context context, int textViewResourceId,
    ArrayList<Country> countryList) {
   super(context, textViewResourceId, countryList);
   this.countryList = new ArrayList<Country>();
   this.countryList.addAll(countryList);
  }
 
  private class ViewHolder {
  // TextView code;
   CheckBox name;
  }
 
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
 
   ViewHolder holder = null;
   Log.v("ConvertView", String.valueOf(position));
 
   if (convertView == null) {
   LayoutInflater vi = (LayoutInflater)getSystemService(
     Context.LAYOUT_INFLATER_SERVICE);
   convertView = vi.inflate(R.layout.country_info, null);
 
   holder = new ViewHolder();
 //  holder.code = (TextView) convertView.findViewById(R.id.code);
   holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
   convertView.setTag(holder);
 
    holder.name.setOnClickListener( new View.OnClickListener() { 
     public void onClick(View v) { 
      CheckBox cb = (CheckBox) v ; 
      Country country = (Country) cb.getTag(); 
      Toast.makeText(getApplicationContext(),
       "Clicked on Checkbox: " + cb.getText() +
       " is " + cb.isChecked(),
       Toast.LENGTH_LONG).show();
      country.setSelected(cb.isChecked());
     } 
    }); 
   }
   else {
    holder = (ViewHolder) convertView.getTag();
   }
 
   Country country = countryList.get(position);
   //holder.code.setText(" (" +  country.getCode() + ")");
  
   holder.name.setText(country.getName());
   holder.name.setChecked(country.isSelected());
  holder.name.setTag(country);
 
   return convertView;
 
  }
 
 }
 
 private void checkButtonClick() {
 
 
  Button myButton = (Button) findViewById(R.id.findSelected);
  myButton.setOnClickListener(new OnClickListener() {
 
   public void onClick(View v) {
	  
    StringBuffer responseText = new StringBuffer();
    responseText.append("Id");
    Intent intent = getIntent();
    try{
     crp = intent.getStringExtra(SingleListItem.cropname);
    Log.d(crp,"crop name wise");
   
    if(crp.trim().equals("")||crp.equals(null)){
    	Log.d("Inside","StateWiseSearch");
    crp = intent.getStringExtra(StateWiseSearch.cropname);}
    }
    catch(Exception e){
 
    	Log.d("Inside","All Crops");
    crp = "all";}
    Log.d("ListViewCheckboxesActivity","2");
   // responseText.append("The following were selected...\n");
 
    ArrayList<Country> countryList = dataAdapter.countryList;
    for(int i=0;i<countryList.size();i++){
     Country country = countryList.get(i);
     if(country.isSelected()){
    	 String str=new String();
    	 if(country.getName().equalsIgnoreCase("Crop Name"))
    		 str="cropname"; 
    	 else if(country.getName().equalsIgnoreCase("Crop Quality"))
    		 str="cropQuality"; 
    	 else if(country.getName().equalsIgnoreCase("Crop Quantity"))
    		 str="quantity"; 
    	 else if(country.getName().equalsIgnoreCase("BaseCost/Ton(Rs.)"))
    		 str="baseCost"; 
    	 else if(country.getName().equalsIgnoreCase("Current Bid Price(Rs.)"))
    		 str="cost"; 
    	 else if(country.getName().equalsIgnoreCase("Farmer"))
    		 str="fname"; 
    	 else if(country.getName().equalsIgnoreCase("Location(State)"))
    		 str="state"; 
    	 else if(country.getName().equalsIgnoreCase("Package Cost/Ton(Rs.)"))
    		 str="pkgCost"; 
    	
      responseText.append( ","+str);
    	
     }
    }
    Intent j = new Intent(getApplicationContext(), CropTable.class);
	  // sending data to new activity
   if(crp.equalsIgnoreCase(null))
	   j.putExtra(fields, responseText.toString());
	   
   else
	  j.putExtra(fields, responseText.toString()+"@"+crp);
	  //j.putExtra("crops",crp);
	  Log.d("In list View fields",responseText.toString());
	 Log.d("In list View crops",crp);
	  startActivity(j);
  //  Toast.makeText(getApplicationContext(),
    //  responseText, Toast.LENGTH_LONG).show();
 
   }
  });
 
 }
 
}